package com.jmu.bishe.web.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.alibaba.fastjson.JSONObject;
import com.jmu.bishe.entity.Member;
import com.jmu.bishe.service.UserService;
import com.jmu.bishe.service.main.TableService;
import com.jmu.bishe.utils.StringUtils;
import com.jmu.bishe.web.common.BaseController;

@Controller
public class MainController extends BaseController{

	@Autowired
	private TableService service;
	
	@Autowired
	private UserService userService;

	/**
	 * 用户登陆页面
	 * @param model
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/login.do") 
    public String login(Model model,HttpServletRequest request) throws Exception {
		if(request.getSession().getAttribute("user")!=null){
			return "redirect:/admin/myhouse.do";
		}
        return "login";
    }
   
	/**
	 * 用户登陆页面
	 * @param model
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/dologin.do",method=RequestMethod.POST) 
    public String dologin(Model model,HttpServletRequest request,String UserName,String Password) throws Exception {
		logger.info("user:"+UserName+"   Password："+Password);
		//request.getParameter("UserName");
		
		Member m = userService.getMember(UserName,Password);
		
		if(m==null){
			model.addAttribute("error","登录用户或者密码错误！");
			model.addAttribute("userName", UserName);
			return "login";
		}else{
			String redirectUrl = request.getParameter("redirectUrl");
			request.getSession().setAttribute("user", m);
			if(StringUtils.isBlank(redirectUrl)){
				return "redirect:/admin/myhouse.do";
			}else{
				return "redirect:"+redirectUrl;
			}
		}
    }
	
	/**
	 * 用户登陆页面
	 * @param model
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/ajaxlogin.do") 
    public void ajaxlogin(Model model,HttpServletRequest request,HttpServletResponse response,String UserName,String Password) throws Exception {
		logger.info("user:"+UserName+"   Password："+Password);
		//request.getParameter("UserName");
		
		Member m = userService.getMember(UserName,Password);
		JSONObject json = new JSONObject();
		
		if(m==null){
			json.put("status","false");
			
		}else{
			json.put("status","true");
			json.put("member",m);
		}
		// {"name":"黄","age":20}
		printOut(response, json.toJSONString());
    }
	
	/**
	 * 用户退出
	 * @param model
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/logout.do") 
    public String logout(Model model,HttpServletRequest request) throws Exception {
		if(request.getSession().getAttribute("user")!=null){
			request.getSession().removeAttribute("user");
		}
		return "redirect:/login.do";
    }
}
